@extends('admin.layout')

@section('content')

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            用户管理
            <small>用户列表</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
            <li><a href="#">用户管理</a></li>
            <li class="active">列表</li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title">快速查看用户</h3>
                    </div><!-- /.box-header -->
                    <div class="box-body">
                        @if(session('info'))
                        <div id="jg" class="callout callout-info">
                            <p>{{ session('info') }}</p>
                        </div>
                        @endif
                    <form action="{{ url('/admin/user/index') }}">
                    <div class="row">
                        <div class="col-md-2">
                            <div class="form-group">
                                <select name="num" class="form-control">

                                    <option
                                    @if(!empty($request['num']) && $request['num'] == 10)
                                        selected="selected"
                                    @endif
                                    >10</option>
                                    <option
                                    @if(!empty($request['num']) && $request['num'] == 25)
                                        selected="selected"
                                    @endif
                                    >25</option>
                                    <option
                                    @if(!empty($request['num']) && $request['num'] == 50)
                                        selected="selected"
                                    @endif
                                    >50</option>
                                    <option
                                    @if(!empty($request['num']) && $request['num'] == 100)
                                        selected="selected"
                                    @endif
                                    >100</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4 col-md-offset-6">
                            <div class="input-group">
                            <input value="{{ $request['keywords'] or '' }}" name="keywords" type="text" class="form-control">
                                <span class="input-group-btn">
                                    <button class="btn btn-info btn-flat">搜索</button>
                                </span>
                            </div>
                        </div>
                    </div>
                    </form>
                        <table id="example2" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户名</th>
                                <th>邮箱</th>
                                <th>头像</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <!-- 遍历 -->
                            @foreach($data as $v)
                            <tr>
                                <td class="ids">{{$v -> id}}</td>
                                <td class="name">{{$v -> name}}</td>
                                <td>{{$v -> email}}</td>
                                <td><img width="50" src="/uploads/yhtx/{{$v -> pic}}"/></td>
                                <td class="status">
                                    @if($v -> status == 0)
                                    禁用
                                    @elseif($v -> status == 1)
                                    启用
                                    @endif
                                </td>
                               <td><a href="{{url('/admin/user/edit/')}}/{{$v -> id}}">编辑</a> <a href="{{url('/admin/user/delete/')}}/{{$v -> id}}">删除</a></td>
                            </tr>
                            @endforeach
                            <!-- 遍历结束 -->
                            </tbody>
                        </table>
                        {!! $data->appends($request)->render() !!}
                    </div><!-- /.box-body -->
                </div><!-- /.box -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </section><!-- /.content -->
</div><!-- /.content-wrapper -->

<script type="text/javascript">
    window.onload = function()
    {
        // alert($);

        // 隐藏警告消息
        setInterval(function(){
            $('#jg').fadeOut(1000);
        }, 2000);

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
            }
        });
        $('.status').on('click',function(){

            // 获取对象
            var t = $(this);
            // 获取id
            var id = $(this).parent().find('.ids').html();
            // alert(id);
            $.ajax({
                type:"POST",
                url:"{{ url('/admin/user/ajaxStatus') }}",
                data:{id:id},
                success:function(data){
                    if(data == 2)
                    {
                        alert('修改失败');
                    }else if(data == 0)
                    {
                        t.html('禁用');
                    }else if(data == 1)
                    {
                        t.html('启用');
                    }
                },
                error:function()
                {
                    alert('异常');
                }
            });
        });
        // 双击修改用户名
        $(".name").on('dblclick', dblClick);
        function dblClick()
        {
            var t = $(this);
            // 获取id
            var id = $(this).parent().find('.ids').html();
            // 准备inp
            var inp = $("<input type='text' />");
            // 获取原来的值
            var oldName = $(this).html();

            // 添加原来的值
            inp.val(oldName);

            // 修改td内容
            $(this).html(inp);
            inp.select();

            $(inp).on('blur',function(){

                // 获取新的inp内容
                var newName = inp.val();
                // alert(newName);
                $.ajax({
                    type:"POST",
                    url:"{{ url('/admin/user/ajaxName') }}",
                    data:{id:id, name:newName},
                    success:function(data){
                        if(data == 4)
                        {
                            alert('用户名已经存在');
                            t.html(oldName);
                        }else if(data == 0){
                            alert('修改失败');
                            t.html(oldName);
                        }else if(data == 1)
                        {
                            t.html(newName);
                        }
                    },
                    error:function()
                    {
                        alert('异常');
                    }
                });
                // 注册事件
                t.on('dblclick', dblClick);
            });
            t.unbind('dblclick');
        }
    }
</script>
@endsection